'use strict';
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : 为客户端上传的参数', event, context)
	const db = uniCloud.database()
	const query = await db.collection("uni-id-device").where({
		user_id: event.userId
	}).get()
	let result = {
		code: 0,
		msg: "执行失败"
	}
	if(query.affectedDocs > 0){
		result = await db.collection("uni-id-device").where({
			user_id: event.userId
		}).update({
			push_clientid: event.cid
		}) 
	}else{
		result = await db.collection("uni-id-device").add({
			user_id: event.userId,
			push_clientid: event.cid,
			create_date: Date.now()
		})
	}
	//返回数据给客户端
	return query
};
